<?php
namespace app\backstage\logic;
use think\Db;

class Payment
{
    public function getList($params)
    {

        $field = 'id,chargefrom,recharged_num,unpaid_num,money,create_date';

        $condition[] = ['chargefrom', 'eq', 'gt:0', 'chargefrom'];

        $where = buildQueryCondition($condition, $params);

        if( isset($params['field']) ){
            $orderBy = [ $params['field'] => $params['order'] ];
        }else{
            $orderBy = ['create_date' => 'DESC'];
        }

        if (isset($params['start_date']) && strlen($params['start_date']) > 1) {
            $where[] = ['create_date', '>=', $params['start_date']];
        }

        if (isset($params['end_date']) && strlen($params['end_date']) > 1) {
            $where[] = ['create_date', '<=', $params['end_date']];
        }

        $Payment   = \think\facade\App::model('Payment');
        $list  = $Payment->getList($where, $field, $params['page'], $params['limit'], $orderBy);

        if($list)
        {
            $chargefrom_gh = array_unique(array_column($list,'chargefrom'));
            $pays = Db::table('quick_payment_number')->where('id','in',$chargefrom_gh)->column('id,name');
            foreach ($list as $k=>$v)
            {
                $list[$k]['title'] = isset($pays[$v['chargefrom']]) ? $pays[$v['chargefrom']] : '';
                $list[$k]['money'] = getQuotient($v['money'],100);
            }
        }

        $total_price = Db::table('rp_payment_number_daily')->where($where)->sum('money');
        $total_price = $total_price && $total_price > 0 ? $total_price : 0;

        $result['totalRow']['total_money'] = round($total_price / 100, 2);
        $result['count'] = $Payment->getTotalNumber($where);
        $result['list']  = $list;

        return $result;
    }
}